package com.wangjiacai.utils.web.configuration;

import com.github.qcloudsms.SmsSingleSender;
import com.wangjiacai.utils.mail.MailSenderService;
import com.wangjiacai.utils.mail.impl.MailSenderServiceImpl;
import com.wangjiacai.utils.web.filter.SystemFilter;
import com.wangjiacai.utils.web.filter.TokenFilter;
import com.wangjiacai.utils.web.properties.FilterProperties;
import com.wangjiacai.utils.web.properties.MailProperty;
import com.wangjiacai.utils.web.properties.SmsProperty;
import com.wangjiacai.utils.web.service.RedisService;
import com.wangjiacai.utils.sms.DefaultSmsCodeSender;
import com.wangjiacai.utils.sms.SmsCodeSender;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/*
 * @auther: wangjiacai
 *
 * @create: 2019-01-27 14:47:17
 */
@Configuration
@EnableConfigurationProperties({ SmsProperty.class, FilterProperties.class,MailProperty.class})
@ComponentScan(basePackages = "com.wangjiacai.utils.web")
public class WebUtilsConfiguration {

    @Bean
    @ConditionalOnMissingBean(PasswordEncoder.class)
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    @ConditionalOnMissingBean(SmsSingleSender.class)
    public SmsSingleSender smsSingleSender(SmsProperty smsProperty){
        int appId=smsProperty.getAppId();
        String appKey=smsProperty.getAppKey();
        SmsSingleSender sender = new SmsSingleSender(appId, appKey);
        return sender;
    }

    @Bean
    @ConditionalOnMissingBean(SmsCodeSender.class)
    @ConditionalOnBean(SmsSingleSender.class)
    public SmsCodeSender smsCodeSender(){
        return new DefaultSmsCodeSender();
    }

    @Bean
    @ConditionalOnMissingBean(JavaMailSender.class)
    public JavaMailSender javaMailSender(MailProperty mailProperty){
        JavaMailSenderImpl javaMailSender= new JavaMailSenderImpl();
        javaMailSender.setHost(mailProperty.getHost());
        javaMailSender.setPort(mailProperty.getPort());
        javaMailSender.setUsername(mailProperty.getUsername());
        javaMailSender.setPassword(mailProperty.getPassword());
        return javaMailSender;
    }

    @Bean
    @ConditionalOnMissingBean(MailSenderService.class)
    @ConditionalOnBean(JavaMailSender.class)
    public MailSenderService mailSenderService(){
        return new MailSenderServiceImpl();
    }



    @Bean
    @ConditionalOnMissingBean(name = "systemFilter")
    public FilterRegistrationBean systemFilter(FilterProperties filterProperties) {
        FilterRegistrationBean registration = new FilterRegistrationBean(new SystemFilter(filterProperties));
        registration.addUrlPatterns("/*");
        registration.setName("systemFilter");
        return registration;
    }

    @Bean
    @ConditionalOnMissingBean(name = "tokenFilter")
    public FilterRegistrationBean tokenFilter(RedisService redisService, FilterProperties filterProperties) {
        FilterRegistrationBean registration = new FilterRegistrationBean(new TokenFilter(filterProperties,redisService));
        registration.addUrlPatterns("*");
        registration.setName("tokenFilter");
        return registration;
    }

    public static void main(String[] args) {

    }
}
